<?php
/**
 * @link https://www.kancloud.cn/cleverstone/ymb2
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\tree_table\util\tttcustom;

use builder\tree_table\util\tttditems\TDItemsType;
use yii\base\InvalidArgumentException;

class Dropdown extends TCustomType
{
    /**
     * @var string 位置：left(顶部左边)、right(顶部右边)、bottom(尾部)
     */
    private $_position = 'left';

    /**
     * @var string 按钮标题
     */
    private $_title = '';

    /**
     * @var string 按钮图标
     */
    private $_icon = '';

    /**
     * @var string 按钮class样式
     */
    private $_btnClass = 'layui-btn-primary';

    /**
     * @var array 选项
     */
    private $_options = [
        'type' => 'dropdown', // 支持：page(页面)、modal(模态框)、ajax(AJAX)、dropdown(下拉菜单)
        'items' => [], // 下拉菜单数据项
    ];

    /**
     * 位于表格顶部右侧
     * @return $this
     */
    public function positionRight()
    {
        $this->_position = 'right';
        return $this;
    }

    /**
     * 位于表格尾部
     * @return $this
     */
    public function positionBottom()
    {
        $this->_position = 'bottom';
        return $this;
    }

    /**
     * @param string $title 名称标题
     * ```php
     * // 文本标题
     * $this->title('编辑');
     *
     * // 支持HTML
     * $this->title(H5::text('编辑'));
     *
     * ```
     *
     * @return $this
     */
    public function title($title)
    {
        $this->_title = (string)$title;
        return $this;
    }

    /**
     * @param string $icon 按钮图标
     * 支持的图标有:
     * - bootstrap v4图标
     * - fontawesome v5
     * - layui 2.8图标
     *
     * @return $this
     */
    public function icon($icon)
    {
        $this->_icon = $icon;
        return $this;
    }

    /**
     * @param array|string $btnClass 按钮class样式
     * @return $this
     */
    public function btnClass($btnClass)
    {
        $this->_btnClass = $btnClass;
        return $this;
    }

    /**
     * 下拉菜单数据项
     * ```php
     * $this->ttToolbarDropdown([
     *      $this->ttToolbarDropdown->model()->title()->route()->width()->height(),
     *      $this->ttToolbarDropdown->model()->title()->route()->width()->height(),
     * ]);
     * ```
     * @param array $data
     * @return $this
     */
    public function ttToolbarDropdown(array $data)
    {
        $tmp = [];
        foreach ($data as $value) {
            if (!$value instanceof TDItemsType) {
                throw new InvalidArgumentException('The TreeTable toolbar dropdown-items must be instanced of abstract class [builder\tree_table\util\tttditems\TDItemsType]. ');
            }
            $tmp[] = $value->_toArray();
        }

        $this->_options['items'] = $tmp;
        return $this;
    }

    /**
     * @return array
     */
    public function _toArray()
    {
        return [
            'position' => $this->_position,
            'title' => $this->_title,
            'icon' => $this->_icon,
            'btnClass' => $this->_btnClass,
            'options' => $this->_options,
        ];
    }
}